// pages/shopping/order/details.js
const app = getApp()
const connector = require('../../../utils/connector.js');
const myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(18[0-9]{1})|(19[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
const format = /^(([1][1-5])|([2][1-3])|([3][1-7])|([4][1-6])|([5][0-4])|([6][1-5])|([7][1])|([8][1-2]))\d{4}(([1][9]\d{2})|([2]\d{3}))(([0][1-9])|([1][0-2]))(([0][1-9])|([1-2][0-9])|([3][0-1]))\d{3}[0-9xX]$/;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bar_Height: wx.getSystemInfoSync().statusBarHeight + 45,
    phone:"",
    userName:"",
    idcard:"",
    school:"",
    arr:[]
  },
  userNameInput(e){
    this.setData({
      userName:e.detail.value
    })
  },
  phone(e){
    this.setData({
      phone:e.detail.value
    })
  },
  idcard(e){
    this.setData({
      idcard:e.detail.value
    })
  },
  school(e){
    this.setData({
      school:e.detail.value
    })
  },
  payment(e){
    let that = this
    let arr = that.data.arr
    let obj = {}
    for(let i=0;i<arr.length;i++){
      if(arr[i] == "name"){
        if(!that.data.userName){
          connector.showToasts("请输入姓名")
          return false
        }else{
          obj.name = that.data.userName
        }
      }
      if(arr[i] == "phone"){
        if(!that.data.phone){
          connector.showToasts("请输入联系电话")
          return false
        }else{
          if (that.data.phone.length < 11 || !myreg.test(that.data.phone)) {
            connector.showToasts("联系电话输入有误")
            return false
          }else{
            obj.phone = that.data.phone
          }
        }
      }
      if(arr[i] == "IDcard"){
        if(!that.data.idcard){
          connector.showToasts("请输入身份证号")
          return false
        }else{
          if (that.data.idcard.length < 11 || !format.test(that.data.idcard)) {
            connector.showToasts("身份证号输入有误")
            return false
          }else{
            obj.IDcard = that.data.idcard
          }
        }
      }
      if(arr[i] == "school"){
        if(!that.data.school){
          connector.showToasts("请输入学校名")
          return false
        }else{
          obj.school = that.data.school
        }
      }
    }
   
    
    
    
    let arrNew = that.data.accounts;
    obj.goods_id = arrNew[0].id
    // console.log(obj)
    // let userAddreObj = {
    //   name: that.data.userName,
    //   phone: that.data.phone,
    //   goods_id: arr[0].id
    // }
    sundryAdd(that, obj)

      let arrs = [
        {
          product_id:arrNew[0].id,
          count:1,
          type:4,
        }
      ];
      //下单
      wx.request({
        url: app.conUser + '/order/place',
        data: {
          order_type:4,
          products: arrs,
        },
        header: {
          token: app.tokens
        },
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        success: function (res) {
          // console.log(res)
          if(res.data.order_id > 0){
            wx.setStorageSync('order_id', res.data.order_id);
            // console.log(res.data.order_id)
            // console.log(res.data)
            if (app.tokens) {
              //拉取微信支付
              wx.request({
                url: app.conUser + '/pay/pre_order',
                method: 'POST',
                header: {
                  token: app.tokens
                },
                data: {
                  id: res.data.order_id
                },
                success: function (ser) {
                  // console.log(ser)
                  var preData = ser.data;
                  // console.log(preData)
                  headlineBuyDelete()
                  //拉取微信支付
                  wx.requestPayment({
                    timeStamp: preData.timeStamp.toString(),
                    nonceStr: preData.nonceStr,
                    package: preData.package,
                    signType: preData.signType,
                    paySign: preData.paySign,
                    success: function (sss) {
                      // console.log(sss)
                      // wx.navigateTo({
                      //   url: '/pages/oneself/order/order',
                      // })
                      connector.showToasts("支付成功")
                      setTimeout(function(){
                        wx.navigateBack({
                          delta: 1,
                        })
                      },1500)
                      // wx.navigateBack({
                      //   delta: 1,
                      // })
                    },
                    fail: function (error) {
                      connector.showToasts("您取消了支付")
                      setTimeout(function(){
                        wx.navigateBack({
                          delta: 1,
                        })
                      },1500)
                      
                      // console.log(error)
                      // wx.navigateTo({
                      //   url: '/pages/oneself/order/order',
                      // })
                      // console.log(res.data.order_no)
                      // wx.redirectTo({
                      //   url: '/pages/oneself/order/details/details?queryBean=' + JSON.stringify(res.data.order_no),
                      // })
                    }
  
                  })
  
                }
              })
            }
          }else{
            wx.showToast({
              title: "下单失败,请联系客服",
              icon: 'none',
              duration: 1000
            })
          }
          
        },
        fail: function (res) {
        }
      });

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log(wx.getStorageSync('headlineBuy'))
    this.setData({
      accounts:wx.getStorageSync('headlineBuy'),
      arr:wx.getStorageSync('headlineBuy')[0].parameter
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
/**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {
    var that = this
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  }
})
//上传地址
function sundryAdd(that, address) {
  // const shoppingIds = {
  //   product_id: shoppingId
  // }
  // console.log(address)
  connector.getFunc(connector.connector().sundryAdd, address, "post", app.tokens, function (res) {
    if (res.statusCode == 200) {
      // console.log(res.data)
      // console.log(res)

    }
  }, function (res) {
    wx.showToast({
      title: "内部错误",
      icon: 'none',
      duration: 1000
    })
  })
}
//清空
function headlineBuyDelete(){
  wx.setStorageSync('headlineBuy',[])
}